package store

import (
	"codejy/structdef"
	"fmt"
)

// 创建RecordLog
func NewRecordLog(threadStore *structdef.ThreadStore, lineCodeDefinition structdef.LineCodeDefinition) structdef.RecordLog {
	return structdef.RecordLog{ThreadStore: threadStore, LineCodeDefinition: lineCodeDefinition}
}

// 添加错误日志代码记录
func AddErrorLog(threadStore *structdef.ThreadStore, error structdef.RecordLog) {
	threadStore.ExecuteCodeRecordLogMap = append(threadStore.ExecuteCodeRecordLogMap, error)
}

// 清理错误日志
func ClearErrorLog(threadStore *structdef.ThreadStore) {
	threadStore.ExecuteCodeRecordLogMap = []structdef.RecordLog{}
}

// 格式化输出错误日志
func PrintExecuteCodeRecordLog(threadStore *structdef.ThreadStore, err any) {

	fmt.Println("::::::代码执行跟踪:")

	for _, error1 := range threadStore.ExecuteCodeRecordLogMap {
		//如果是core&CreateTypeDefault()函数,那么不打印
		if len(error1.LineContent) > 6 && (error1.LineContent[:4] == "core" || error1.LineContent[:6] == "tocore") {
			continue
		}
		fmt.Printf("文件名:%s 行号:%d 执行的代码:%s \n", error1.LineCodeFile, error1.LineNum, PrintExecuteCodeRecordLogFormat(error1.LineContent))
	}
	ClearErrorLog(threadStore)
	if err != nil {
		fmt.Printf("::::::发生报错原因提示信息: %s \n", err)

	}
}

// 输出格式化的日志
func PrintExecuteCodeRecordLogFormat(codeLine string) string {
	//如果开头是var ,那么在var后面加上一个空格
	if codeLine[:3] == "var" {
		codeLine = "var " + codeLine[3:]
	}
	return codeLine
}
